package com.sugon.task;


import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.ShiroSecurityManagerChecker;
import com.sugon.entity.ModuleSpStockEntity;
import com.sugon.service.ModuleSpDeptStockService;
import com.sugon.service.ModuleSpStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模具型芯车间库存统计任务
 *
 * @author YuXD
 * @since 2022-05-18
 */
@Slf4j
@Component("moldSpDeptStockStatTask")
public class MoldSpDeptStockStatTask extends AbstractBaseProcessTask {

    @Resource
    private ModuleSpStockService moldSpStockService;
    @Resource
    private ModuleSpDeptStockService moduleSpDeptStockService;

    @ShiroSecurityManagerChecker
    @Override
    public void process() {
        List<ModuleSpStockEntity> moldSpStockList = moldSpStockService.queryList(null);
        Map<String, List<ModuleSpStockEntity>> deptMoldSpStockMap = moldSpStockList.stream().filter(stock -> StrUtil.isNotEmpty(stock.getWorkshop())).collect(Collectors.groupingBy(ModuleSpStockEntity::getWorkshop));
        deptMoldSpStockMap.forEach((dept, deptMoldSpStockList) -> {
            Map<String, List<ModuleSpStockEntity>> moldSpStockMap = deptMoldSpStockList.stream().collect(Collectors.groupingBy(ModuleSpStockEntity::getMoldSpId));
            moldSpStockMap.forEach((moldSpId, spStockList) ->
                    moduleSpDeptStockService.realtimeUpdateDeptStock(dept, moldSpId)
            );
        });
    }

}
